		
<?php

require_once  __DIR__ . '/includes/authenticate.php';

include_once __DIR__ . '/db_bb_connect.php';

require_once __DIR__ . '/includes/BasketbalFunctions.php';

$basketbalfunctions = new BasketbalFunctions();

?>
		<table class="table table-hover table-striped table-condensed">
		    <thead>
		    	</thead>
		    <tbody>
		         		    
		      	<?php
		      	if (isset($_POST['visible_only'])) {
		      		$sql = "SELECT DISTINCT *
				    FROM video_feedback 
				    JOIN video_feedback_visibility
				    ON video_feedback.ID = video_feedback_visibility.video_ID
				    WHERE video_feedback.archived = 'no'
				    ORDER BY video_feedback_visibility.date_create DESC";
		      	} else {
		      		$sql = "SELECT *
				    FROM video_feedback
				    WHERE archived = 'no'
				    ORDER BY date_create DESC";
		      	}


					if ($db->query($sql)) {  
						$result = $db->query($sql);
						while ($row = $result->fetch_assoc()) {
							$text = nl2br($row['comment']);
							$time_start = $row['time_start'];
							$time_end = $row['time_end'];
							$duration = $time_end - $time_start;
							$ID = $row['ID'];
							$season = $row['season'];
							$title = $row['title'];

							$gamenumber = $row['gamenumber'];

							$tags = $row['action_tags'];
							$players = $row['players'];	

							$date_create = $row['date_create'];
							$added_by_user_key = $row['added_by'];
							$added_by_username = $basketbalfunctions->GetUserFirstnameFromUserkey($added_by_user_key);

							$gamenamestring = $basketbalfunctions->GetGameOpponentLocationDate($season, $gamenumber);
				?>
							<tr style="background: #3a3939;height: 10px;"><td colspan="6" ></td></tr>
							<tr id="tablerow<?= $ID?>"> 
							<td> 
								<div id="player<?= $ID?>" align='center'> <button class="btn btn-sm btn-default loadvideo<?= $ID?>">Load Video</button>

 			 				</div> 
							</td>
							<td id="bg<?= $ID?>"> 

								<strong>Game:</strong> <?= $gamenamestring ?>
								<br>
								<strong>
								Title: 
							</strong> <?= $title ?> 
							<br>
								<strong> Tags: 
								<?php 

							        $tags_split = explode(",", $tags);
									foreach ($tags_split as $tag_ID) {
										$tag_name = $basketbalfunctions->GetActionTagNameFromActionTagID($tag_ID);										
										echo "<span class=\"label label-default\">$tag_name</span> ";
										}					

								?>
								
							</strong> <br>

								<strong> Players: 
								<?php 

							        $players_split = explode(",", $players);
									foreach ($players_split as $user_key) {
										$player_firstname = $basketbalfunctions->GetUserFirstnameFromUserkey($user_key);
										$player_lastname = $basketbalfunctions->GetUserLastnameFromUserkey($user_key);
										echo "<span class=\"label label-default\">$player_firstname $player_lastname</span> ";
										}					

								?></strong> <br>
								<strong> Current Visibility:</strong> 
								<?php 
								$visibility = $basketbalfunctions->ShowVideoFeedbackVisibilityNamelist($ID); 
								
								echo $visibility;
								
								if ( $visibility != 'nobody') {
									
									?>
									<script type="text/javascript">
										document.getElementById("bg<?= $ID?>").style.backgroundColor = "#dde5d0"; 
									</script>
									<?php
								} else {?>

																		<script type="text/javascript">
										document.getElementById("bg<?= $ID?>").style.backgroundColor = "#ffe6e6"; 
									</script>
									<?php
									
								}


								?> 


								<br>

								<strong> Comment:</strong> <div class="well  well-sm"> <?= $text ?>	</div>
								
								<div class="small text-muted"><em>Added by <?= $added_by_username ?> on <?= $date_create ?></em></div>
						</td>
						<td> 

						<form method="post" action="./video_feedback_edit_entry.php"> 
						<input type="hidden" name="ID" value="<?=$ID?>"/>
						<button class="btn btn-sm btn-success" title="Edit"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> </button>
						
						</form>
						</td>
						<td> 
						<form action="./video_feedback_entry_visibility.php" method="post">
						 <input type="hidden" name="video_ID" value="<?= $ID ?>" ><button class="btn btn-sm btn-info" type="submit" title="Visibility" ><span class="glyphicon glyphicon-eye-open" aria-hidden="true" ></span></button>
						</form>
						</td>
						<td> 
						<form> 
						<input type="hidden" name="ID" value="<?=$ID?>"/>
						<button class="btn btn-sm btn-warning archive_video_feedback_entry" title="Archive"><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span></button>
						</form>
						</td>
						<td> 
						<form> 
						<input type="hidden" name="ID" value="<?=$ID?>"/>
						<button class="btn btn-sm btn-danger delete_video_feedback_entry" title="Delete"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> </button>
						</form></td>

							</tr> 
							<tr style="background: #3a3939;height: 10px;"><td colspan="6" ></td></tr>
				<?php		}		
					} else {
			      		echo "Error: " . $sql . "<br>" . $db->error;
			    		} 				    
		      	?>
		    
		    </tbody>
		</table>

		<script type="text/javascript">

var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      <?php 

      $result = $db->query($sql);

      while ($row = $result->fetch_assoc()) { 

      	$ID = $row['ID'];

       ?>
      var player<?= $ID?>;
      var clicked<?= $ID?> = false;	
      <?php } ?>     

      	<?php 

      $result = $db->query($sql);

      while ($row = $result->fetch_assoc()) { 

      	$season = $row['season'];
      	$gamenumber = $row['gamenumber'];

      	$embed_code = $basketbalfunctions->GetGameEmbedCode($season, $gamenumber);

      	$ID = $row['ID']; 

       ?>

       $('.loadvideo<?= $ID?>').on("click", function(){

	       function onYouTubeIframeAPIReady() {
				player<?= $ID?> = new YT.Player('player<?= $ID?>', {
	          	height: '150',
	          	width: '250',
	          	videoId: '<?= $embed_code?>',
	          	events: {
	            	'onStateChange': onPlayer<?= $ID?>StateChange
	          	}
		    });

        	}

        	onYouTubeIframeAPIReady(); 

    	}); 

        <?php } ?>

      

      	<?php 

      $result = $db->query($sql);

      while ($row = $result->fetch_assoc()) { 

      	$ID = $row['ID'];
      	$time_start = $row['time_start'];
		$time_end = $row['time_end'];
		$duration = ($time_end - $time_start)*1000;

       ?>

      function onPlayer<?= $ID?>StateChange(e) { 
      	if( !clicked<?= $ID?> ){	 
			  clicked<?= $ID?> = true;
			  player<?= $ID?>.seekTo(<?= $time_start?>); 
			  loop<?= $ID?>Video();
			  e.preventDefault(); 
		  };
	  
		  }


	function loop<?= $ID?>Video(){
		  var timeout = <?= $duration?>;
		  setTimeout(seek<?= $ID?>Video, timeout);          
        }

    function seek<?= $ID?>Video(){
    	player<?= $ID?>.seekTo(<?= $time_start?>);
    	loop<?= $ID?>Video();
    }

	function myFunction() {
		    window.print();
	}

<?php } ?>
		  
	$(".delete_video_feedback_entry").on("click", function(e) {
	    postData = $(this.form).serialize();
	    var ID = $(this.form).find('input[name="ID"]').val();
	    if( confirm("Are you sure to delete this feedback item?")){
		    	$.ajax({
		      type: "POST",
		        url: "./includes/handlers/video_feedback_delete_entry_handler.php",
		        data: postData,
		        success: function(data) {    
		          $("#tablerow" + ID).html(data);
		        },
		        error: function (xhr, status, error) {
		          $("#tablerow" + ID).html(xhr.responseText);
		        }
		      }); 
		   }
		    e.preventDefault();
		});

	$(".archive_video_feedback_entry").on("click", function(e) {
	    postData = $(this.form).serialize();
	    var ID = $(this.form).find('input[name="ID"]').val();
	    if( confirm("Are you sure to archive this feedback item?")){
		    	$.ajax({
		      type: "POST",
		        url: "./includes/handlers/video_feedback_archive_entry_handler.php",
		        data: postData,
		        success: function(data) {    
		          $("#tablerow" + ID).html(data);
		        },
		        error: function (xhr, status, error) {
		          $("#tablerow" + ID).html(xhr.responseText);
		        }
		      }); 
		   }
		    e.preventDefault();
		});

</script>